<template>
  <div class="Login-container">
    <!-- 居中的登录盒子 -->
    <div class="login-box" @keyup.enter="loginBtn">
      <div class="avatar-box">
        <img src="@/assets/logo.png" alt="" />
      </div>
      <el-form ref="LoginFormRef" class="login_form" :model="loginForm" :rules="login_rules">
        <!-- 用戶名 -->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user-solid" v-model="loginForm.username"></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="loginForm.password" show-password></el-input>
        </el-form-item>

        <!-- 登录重置按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="loginBtn">登录</el-button>
          <el-button type="info" @click="loginReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'MyLogin',
  data() {
    return {
      // 登录表单的数据绑定对象
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单的校验对象
      login_rules: {
        // 验证用户名是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '用户名长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 12, message: '密码长度在 5 到 12 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击按钮对登录信息进行重置
    loginReset() {
      // 直接将值赋值为空
      // this.loginForm.username = ''
      // this.loginForm.password = ''
      // 通过form组件提供的默认方法，需要通过ref获取DOM元素
      this.$refs.LoginFormRef.resetFields()
    },
    // 表单预验证，验证通过vaild的值为true,然后就可以发起请求
    loginBtn() {
      this.$refs.LoginFormRef.validate(async (vaild) => {
        if (!vaild) return
        const { data: res } = await this.$http.post('login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败！')
        this.$message.success('登录成功！')
        // 将返回的token值存到 sessionStorage中
        sessionStorage.setItem('token', res.data.token)
        // 跳转到主页
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.Login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #2b4b6b;
  height: 100%;
  .login-box {
    position: relative;
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    .avatar-box {
      position: absolute;
      top: 0;
      left: 50%;
      transform: translate(-50%, -50%);
      height: 130px;
      width: 130px;
      overflow: hidden;
      border-radius: 50%;
      background-color: #fff;
      border: 1px soild #eee;
      // 盒子阴影
      box-shadow: 0px 0px 20px -4px #ddd;
      padding: 10px;
      img {
        display: block;
        width: 100%;
        border-radius: 50%;
        background-color: rgba(234, 236, 233);
      }
    }
  }
  // 表单样式
  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0px 20px;
    box-sizing: border-box;
    .btns {
      float: right;
      // display: flex;
      // justify-content:flex-end;
    }
  }
}
</style>
